package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationBatchDO;
import com.jinzhi.jzweb.domain.ExaminationPaperDO;
import com.jinzhi.jzweb.domain.ExaminationQuestionsDO;
import com.jinzhi.jzweb.service.ExaminationBatchService;
import com.jinzhi.jzweb.service.ExaminationPaperService;
import com.jinzhi.jzweb.service.ExaminationQuestionsService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 考试试题
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationQuestions")
public class ExaminationQuestionsController extends BaseController {
	@Autowired
	private ExaminationQuestionsService examinationQuestionsService;
	@Autowired
	private ExaminationPaperService examinationPaperService;
	@Autowired
	private ExaminationBatchService examinationBatchService;
	
	@GetMapping("/{id}")
	@RequiresPermissions("jzweb:examinationQuestions:examinationQuestions")
	String ExaminationQuestions(@PathVariable("id") Long id,Model model){
		model.addAttribute("epaperId",id);
	    return "jzweb/examinationQuestions/examinationQuestions";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:examinationQuestions:examinationQuestions")
	public Result<Page<ExaminationQuestionsDO>> list(ExaminationQuestionsDO examinationQuestionsDTO){
		// 查询列表数据
		Page<ExaminationQuestionsDO> page = examinationQuestionsService.selectPage(getPage(ExaminationQuestionsDO.class),
				examinationQuestionsService.convertToEntityWrapper("epaper_id", examinationQuestionsDTO.getEpaperId(),"introduce", examinationQuestionsDTO.getIntroduce()
				).orderBy("id", true));
		//查询所属竞赛
		ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(examinationQuestionsDTO.getEbatchId());
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ExaminationQuestionsDO obj = (ExaminationQuestionsDO) page.getRecords().get(i);
			if (obj.getEpaperId() != null){
				ExaminationPaperDO examinationPaperDO = examinationPaperService.selectById(obj.getEpaperId());
				if (examinationPaperDO != null) {
					obj.setEpaperName(examinationPaperDO.getName());
				}
			}
			obj.setEbatchName(examinationBatchDO.getName());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add/{id}")
	@RequiresPermissions("jzweb:examinationQuestions:add")
	String add(@PathVariable("id") Long id,Model model){
		model.addAttribute("epaperId",id);
	    return "jzweb/examinationQuestions/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:examinationQuestions:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ExaminationQuestionsDO examinationQuestions = examinationQuestionsService.selectById(id);
		model.addAttribute("examinationQuestions", examinationQuestions);
	    return "jzweb/examinationQuestions/edit";
	}
	
	@Log("添加考试试题")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:examinationQuestions:add")
	public Result<String> save( ExaminationQuestionsDO examinationQuestions){
		/**
		 * 单项选择题 30小题 每小题0.3分，共9分
		 * 多项选择题 10小题 每小题0.8分，共8分
		 * 判断题 10小题 每小题0.3分 共3分
		 * 分数先按十倍处理
		 */
		if(null!=examinationQuestions.getType()){
			switch (examinationQuestions.getType()){
				case  0:
				case  2:
					examinationQuestions.setScore(3);
					break;
				case 1:
					examinationQuestions.setScore(8);
					break;
			}
		}
		examinationQuestionsService.insert(examinationQuestions);
        return Result.ok();
	}
	
	@Log("修改考试试题")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:examinationQuestions:edit")
	public Result<String>  update( ExaminationQuestionsDO examinationQuestions){
		boolean update = examinationQuestionsService.updateById(examinationQuestions);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除考试试题")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationQuestions:remove")
	public Result<String>  remove( Long id){
		examinationQuestionsService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除考试试题")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationQuestions:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		examinationQuestionsService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
